package com.baselibrary.libs.View.bar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.baselibrary.libs.R;
import com.baselibrary.libs.View.baseView.BaseView;


/**
 * Created by 35876 于萌萌
 * 创建日期: 15:50 . 2016年10月03日
 * 描述:
 * <p>
 * <p>
 * 备注: 注意请看这里 id 叫 action_bar
 */
public class initAction_Bar extends BaseView implements View.OnClickListener {
    private ImageView action_bar_more;
    Action_bar_callBack callBack;
    private Button action_bar_rightbutton;
    private Button action_bar_leftbutton;
    private TextView mAction_bar_title;

    public initAction_Bar(Context context) {
        super(context);
    }

    public initAction_Bar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public initAction_Bar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public int getXml() {
        return R.layout.activity_title;
    }

    @Override
    public void initView(View view) {
        //标题文字
        mAction_bar_title = (TextView) view.findViewById(R.id.action_bar_title);
        //右面按钮
        action_bar_rightbutton = (Button) view.findViewById(R.id.action_bar_rightbutton);
        //左面按钮
        action_bar_leftbutton = (Button) view.findViewById(R.id.action_bar_leftbutton);
        //更多按钮
        action_bar_more = (ImageView) view.findViewById(R.id.action_bar_more);


        //默认显示
        mAction_bar_title.setVisibility(VISIBLE);
        action_bar_leftbutton.setVisibility(VISIBLE);

        //默认不显示
        action_bar_rightbutton.setVisibility(GONE);
        action_bar_more.setVisibility(GONE);

        //设置点击事件
        mAction_bar_title.setOnClickListener(this);
        action_bar_leftbutton.setOnClickListener(this);
        action_bar_rightbutton.setOnClickListener(this);
        action_bar_more.setOnClickListener(this);

        //设置默认返回按按键的样式
        set_icon(action_bar_leftbutton,R.drawable.title_leftbutton);

    }

    public interface Action_bar_callBack {
        void getTitle(TextView title);
        void getRightButton(Button rightButton);
        void getLeftButton(Button leftButton);
        void getBarMore(ImageView barMore);

    }



    public void setCallBack(Action_bar_callBack callBack) {
        this.callBack = callBack;
        if (mAction_bar_title.getVisibility() == VISIBLE) {
            callBack.getTitle(mAction_bar_title);
        }
        if (action_bar_rightbutton.getVisibility() == VISIBLE) {
            callBack.getRightButton(action_bar_rightbutton);
        }
        if (action_bar_leftbutton.getVisibility() == VISIBLE) {
            callBack.getLeftButton(action_bar_leftbutton);
        }
        if (action_bar_more.getVisibility() == VISIBLE) {
            callBack.getBarMore(action_bar_more);
        }

    }



    /**
     * 设置标题
     * @param title
     * @return
     */
    public initAction_Bar set_Title(String title){
        set_TextView_text(mAction_bar_title,title);
        return this;
    }


    /**
     * 设置左面按钮的图标
     * @param drawableId
     * @return
     */
    public initAction_Bar set_Leftbutton_Icon(int drawableId){
        set_icon(action_bar_leftbutton,drawableId);
        return this;
    }
    /**
     * 设置右按钮是否显示
     * @param b
     * @return
     */
    public initAction_Bar set_Visibility_rightbutton(boolean b){
        set_Visibilitys(action_bar_rightbutton,b);
        return this;
    }
    /**
     * 设置右按钮是否显示
     * @param b
     * @return
     */
    public initAction_Bar set_Visibility_title(boolean b){
        set_Visibilitys(mAction_bar_title,b);
        return this;
    }
    /**
     * 设置右按钮是否显示
     * @param b
     * @return
     */
    public initAction_Bar set_Visibility_more(boolean b){
        set_Visibilitys(action_bar_more,b);
        return this;
    }
    /**
     * 设置左按钮是否显示
     * @param b
     * @return
     */
    public initAction_Bar set_Visibility_Leftbutton(boolean b){
        set_Visibilitys(action_bar_leftbutton,b);
        return this;
    }



    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.action_bar_leftbutton) {
            //如果没设置回调之后我就 设置 点击返回键是关闭
            if (null == callBack)
                getActivity().finish();
        }
    }
}
